package com.zlm.handler.security;

import com.alibaba.fastjson.JSON;
import com.zlm.domain.ResponseResult;
import com.zlm.enums.AppHttpCodeEnum;
import com.zlm.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zlm
 * @date 2024/12/10
 */
@Slf4j
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) {
        log.error("授权失败！{}",e.getMessage());
        ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        //响应
        WebUtils.renderString(httpServletResponse, JSON.toJSONString(result));
    }
}
